﻿using System;
using System.Collections.Generic;
using System.Text;
using Quanlynhasach.hethongluutru;
using Quanlynhasach.hethongxuly;
using System.Data;

namespace Quanlynhasach.hethongluutru
{
    class KhachHangDAO
    {
        public static DataTable LayDanhSachKhachHang()
        {
            string sql;
            sql = "Select MaKhachHang,TenKhachHang,DienThoai,DiaChi,Email,TienNo From KhachHang ";
            DataTable dt = database.ExcuQuery(sql);

            return dt;
        }

        public static String LayTenKhachHang(String mkh)
        {
            string sql;
            sql = "Select TenKhachHang From KhachHang where MaKhachHang =" + mkh;
            DataTable dt = database.ExcuQuery(sql);
            String ten = dt.Rows[0].ItemArray[0].ToString();

            return ten;
        }

        public void ThemKhachHang(KhachHangDTO khDTO)
        {
            string sql;
            sql = "Insert into KhachHang(TenKhachHang,DienThoai,DiaChi,Email) values ('" +khDTO.TenKhachHang +"','"+khDTO.DienThoai+"','"+khDTO.DiaChi+"','"+khDTO.Email+"')";
            database.ExcuNonQuery(sql);
        }

      public void XoaKhachHang(KhachHangDTO khDTO )
        {
            string sql;
            sql = "Delete From KhachHang Where MaKhachHang = " +khDTO.MaKhachHang;
            database.ExcuNonQuery(sql);
        }
       
        public void CapNhatKhachHang(KhachHangDTO khDTO)
        {
            string sql;
            sql = "Update KhachHang Set TenKhachHang=('" + khDTO.TenKhachHang + "'),DienThoai=('"+khDTO.DienThoai+"'),Diachi=('"+khDTO.DiaChi+"'),Email=('"+khDTO.Email+"')";
            sql += " Where MaKhachHang = " + khDTO.MaKhachHang;
            database.ExcuNonQuery(sql);
        }

     /*   public void LayThongTinKhachHang(int maKhachHang)
        {
            string sql;
            DataTable dt = new DataTable();
            sql = "Select * From KhachHang Where MaKhachHang = " +maKhachHang.ToString();
            database.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                KhachHangDTO khDTO = new KhachHangDTO();
               
                khDTO.MaKhachHang= Convert.ToInt32(dt.Rows[0]["MaKhachHang"]);
                khDTO.TenKhachHang = Convert.ToString(dt.Rows[0]["TenKhachHang"]);
                khDTO.DienThoai=Convert.ToString(dt.Rows[0]["DienThoai"]);
                khDTO.DiaChi = Convert.ToString(dt.Rows[0]["DiaChi"]);
                khDTO.Email = Convert.ToString(dt.Rows[0]["Email"]);
               
            }
        }*/
        public static void UpdateTienNo(KhachHangDTO khDTO)
        {
            string sql = "Update  KHACHHANG set TienNo=(" + khDTO.TienNo + ") where MaKhachHang=" + khDTO.MaKhachHang + "";
            database.ExcuNonQuery(sql);
        }
        
    }
}
